import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import './constants.dart' show Constants, AppColors;
import './conversation_page.dart';

enum ActionItems {
  GROUP_CHAT,
  ADD_FRIEND,
  QR_SCAN,
  PAYMENT,
}

class HomeScreen extends StatefulWidget {
  HomeScreen({Key key}) : super(key: key);
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  PageController _pageController;
  int _currentIndex = 0;
  List<Widget> _pages;
  List<NavigationIconView> _navgationViews;

  void initState() {
    super.initState();
    _navgationViews = [
      NavigationIconView(
        title: "微信",
        icon: IconData(
          0xe641,
          fontFamily: Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe9c2,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: "通讯录",
        icon: IconData(
          0xe60a,
          fontFamily: Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe61f,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: "发现",
        icon: IconData(
          0xe653,
          fontFamily: Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe601,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: "我的",
        icon: IconData(
          0xe600,
          fontFamily: Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe638,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
    ];
    _pageController = PageController(initialPage: _currentIndex);
    _pages = [
      ConversationPage(),
      Container(color: Colors.blue),
      Container(color: Colors.green),
      Container(color: Colors.yellow),
    ];
 }

  _buildPopupMenuItem(Widget icon, String title) {
    return Row(
      children: <Widget>[
        icon,
        Container(
          width: 20.0,
        ),
        Text(
          title,
          textAlign: TextAlign.right,
          style: TextStyle(
            color: Colors.white,
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar bottomNavBar = BottomNavigationBar(
      fixedColor: Color(AppColors.TabIconActive),

      /// tabbar 选中颜色处理
      selectedFontSize: 12.0,
      items: _navgationViews.map((NavigationIconView view) {
        return view.item;
      }).toList(),
      currentIndex: _currentIndex,
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        setState(() {
          _currentIndex = index;
          _pageController.animateToPage(_currentIndex,duration: Duration(microseconds: 200),curve: Curves.easeInOut);
        });
      },
    );
    return Scaffold(
      appBar: AppBar(
        title: Text("微信"),
        elevation: 0,
        // backgroundColor: Color(0xff303030),  /// 修改appbar的颜色
        actions: <Widget>[
          PopupMenuButton(
            itemBuilder: (BuildContext context) {
              return <PopupMenuItem<ActionItems>>[
                PopupMenuItem(
                  child: _buildPopupMenuItem(
                    Icon(Icons.group, color: Colors.red),
                    "发起群聊",
                  ),
                  value: ActionItems.GROUP_CHAT,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(
                    Icon(Icons.add_alert, color: Colors.red),
                    "添加朋友",
                  ),
                  value: ActionItems.ADD_FRIEND,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(
                    Icon(Icons.scanner, color: Colors.red),
                    "扫一扫",
                  ),
                  value: ActionItems.QR_SCAN,
                ),
                PopupMenuItem(
                  child: _buildPopupMenuItem(
                    Icon(Icons.group, color: Colors.red),
                    "收付款",
                  ),
                  value: ActionItems.PAYMENT,
                ),
              ];
            },
            icon: Icon(Icons.add),
            onSelected: (ActionItems item) {
              print("您点击了$item");
            },
          ),
        ],
      ),
      body: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return _pages[index];
        },
        controller: _pageController,
        itemCount: _pages.length,
        onPageChanged: (int index){
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: bottomNavBar,
    );
  }
}

class NavigationIconView {
  final BottomNavigationBarItem item;

  NavigationIconView(
      {Key key, String title, IconData icon, IconData activeIcon})
      : item = BottomNavigationBarItem(
          icon: Icon(
            icon,
            size: 22.0,
          ),
          activeIcon: Icon(activeIcon),
          title: Text(title,
              style: TextStyle(
                fontSize: 14.0,
              )),
          backgroundColor: Colors.blue,
        );
}
